package concole;

import remoteService.ClientHelper;

/**
 * @author ljing12 This class is Administrator. An administrator can be built.
 *         And an administrator can changePassword,viewLists,deleteTermFromLIst
 *         finishing time 2013-3-14,19:40;
 * @version2.0 in the iteration 2 .I add the operation to publish the compulsory
 *             course for administrator .and Also I restrict the input format of
 *             the administrator to avoid File Exception Because most operation
 *             will be based on the right format
 */

public class Administrator extends User {
	final int userNumber = 0;
	String password = "admin";
	String userID = "admin";
	ClientHelper clientHelper=new ClientHelper();

	// The Administrator's ID can never be changed;
	public Administrator() {

	}

	public Administrator(String ID, String password) {
		this.ID = ID;
		this.password = password;
	}

	public void startOperation() {
		final int CHANGEPASSWORD = 0;
		final int SHOW = 1;
		final int DELETE = 2;
		final int EXIT = 3;
		final int ADD = 4;

		System.out.println("Welcome Administrator!");
		String adminFunction = "(1)ChangePassword [newPassword](more than 8B\n(2)Show [listName] (studentsList,teachersList,courseList,courseSelected\n";
		adminFunction += "(3)Delete [listName] [num]terms from those lists(4)SelectCourse\n"
				+ "\n(5)Exit";
		System.out
				.println("As the administrator you can do the things following\n"
						+ adminFunction);
		outer: while (true) {
			// the outer is used to end the loop when the administrator want to
			// exit;
			String cmdString = IOHelper.readConsoleNextLine();
			int cmdInt = CommandHelper.getAdminCmd(cmdString);
			switch (cmdInt) {
			case CHANGEPASSWORD:
				this.changePassword(cmdString);
				System.out.println("sucessfully");
				break;
			case SHOW:
				this.showList(cmdString);
				break;
			case DELETE:
				this.delete(cmdString);
				break;
			case EXIT:
				break outer;
			case ADD:
				this.selectCourse(cmdString);
				break;
			default:
				System.out.println("error");
				break;
			}
		}
	}

	/*
	 * A method to changePassword and give special demand to the length of the
	 * password;
	 */
	public String changePassword(String cmdString) {
		
	
		String infoString = cmdString.split(" ")[1];
		String newCode = infoString.substring(1, infoString.indexOf("]"));
		String cmdStringAgain = "admin";
		if (newCode.length() < 8) {
			// Use a recurrence to get the qualified password with enough
			// length;
			System.out
					.println("the password is not safe enough(more than 8 byte) please input again(fomat the same)");
			cmdStringAgain = IOHelper.readConsoleNextLine();
			newCode = changePassword(cmdStringAgain);
			// make sure the final good password will not be overLaped;
		}
		this.password = newCode;
		IOHelper.writeBackToFile("LoginAccounts.txt", this.toStirng());
		return newCode;
	}

	public void showList(String cmdString) {
		String infoString = cmdString.split(" ")[1];
		String listName = infoString.substring(1, infoString.indexOf("]"));
		String fileName = listName + ".txt";
		IOHelper.showAllFile(fileName);
	}

	public void delete(String cmdString) {
		String listNameString = cmdString.split(" ")[1];
		// divide the information from the command
		String listName = listNameString.substring(1, // drop the "[]"
				listNameString.indexOf("]"));
		// get the listName want to be deleted terms from
		String numString = cmdString.split(" ")[2];
		int num = Integer.parseInt(numString.substring(1,
				numString.indexOf("]")));
		// get the lineNumber of which line would be deleted
		String fileName = listName + ".txt";
		IOHelper.deleteOneLine(fileName, num);
	}

	public void selectCourse(String cmdString) {
		System.out
				.println("You only allowed to appoint the compulsory course for the student");

	}

	public void processNetChangePass(String newpassword) {
		String standardcmdString = "AdminOp" + "!" + 1 + "!" + newpassword;
		clientHelper.sendToNet(standardcmdString);
	}

	public String processNetShow(String fileName) {

		String standardcmdString = "AdminOp" + "!" + 2 + "!" + fileName;
		String resultString=clientHelper.sendToNet(standardcmdString);
		resultString=resultString.replace("!", "\n");
		return resultString;
	}

	public void processNetDelete(int lineNum, String fileName) {
		String standardcmdString = "AdminOp" + "!" + 3 + "!" + fileName + "!"
				+ lineNum;
		clientHelper.sendToNet(standardcmdString);
	}

	@Override
	public String getPassword() {
		// TODO Auto-generated method stub
		return this.password;
	}

	// override
	public String toStirng() {
		return userID + " " + password;
	}
}
